<template>
  <div>
    <el-dialog
      :title="status.isAdd ? '秒杀添加' : '秒杀编辑'"
      :visible.sync="status.isshow"
      @closed="closed"
    >
      {{ form }}{{ data }}
      <el-form :model="form">
        <el-form-item label="活动名称" label-width="120px">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动名称" label-width="120px">
          <el-date-picker
            :default-value="data[0]"
            v-model="data"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="timestamp"
          >
          </el-date-picker>
        </el-form-item>
        <!-- 一级分类 -->
        <el-form-item label="一级分类" label-width="120px">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择一级分类"
            @change="change_first_cate_id"
          >
            <!-- 遍历数据 -->
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 二级分类 -->
        <el-form-item label="二级分类" label-width="120px">
          <el-select
            v-model="form.second_cateid"
            placeholder="请选择二级分类"
            @change="change_second_cate_id"
          >
            <!-- 遍历二级分类列表 -->
            <el-option
              v-for="item in secondCateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品" label-width="120px">
          <el-select v-model="form.goodsid" placeholder="商品">
            <!-- 遍历三级分类列表 -->
            <el-option
              v-for="item in goods"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="status.isshow = false">取 消</el-button>
        <el-button type="primary" v-if="status.isAdd" @click="con"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="updata">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  reqcatelist,
  reqgoodslist,
  reqseckadd,
  reqseckinfo,
  reqseckedit,
} from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";

export default {
  components: {},
  props: ["status"],
  data() {
    return {
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: "",
      },
      data: [],
      secondCateList: [],
      goods: [],
    };
  },
  methods: {
    ...mapActions({
      //请求分类的列表
      reqCateList: "cate/reqList",
      reqList: "seck/reqList",
    }),
    //一级分类选中
    change_first_cate_id() {
      //选中的二级分类要重置
      this.form.second_cateid = "";

      //根据一级分类的id获取二级分类的列表
      this.requst_second_cate_list();
    },
    // 获取二级分类
    async requst_second_cate_list() {
      let res = await reqcatelist({ pid: this.form.first_cateid });
      if (res.data.code == 200) {
        this.secondCateList = res.data.list ? res.data.list : [];
      }
    },
    //二级分类选中
    change_second_cate_id() {
      this.goodsid = "";
      //根据一级分类的id获取二级分类的列表
      this.requst_goods_cate_list();
    },
    //获取goods
    async requst_goods_cate_list() {
      let res = await reqgoodslist({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid,
      });
      if (res.data.code == 200) {
        this.goods = res.data.list ? res.data.list : [];
      }
    },
    //添加
    con() {
      this.form.begintime = this.data[0];
      this.form.endtime = this.data[1];
      reqseckadd(this.form).then((res) => {
        this.checkeddata().then(() => {
          if (res.data.code === 200) {
            // 弹成功
            successAlert(res.data.msg);
            // 数据清空
            this.empty();
            // 弹框消失
            this.status.isshow = false;
            // 列表刷新----------此处调用vuex的方法------------------
            this.reqList();
          }
        });
      });
    },
    //验空
    checkeddata() {
      return new Promise((reslove) => {
        if (this.form.title == "") {
          errorAlert("标题不能为空");
          return;
        }
        if (this.begintime == "") {
          errorAlert("时间不能为空");
          return;
        }
        if (this.form.first_cateid == "") {
          errorAlert("一级分类不能为空");
          return;
        }
        if (this.form.second_cateid == "") {
          errorAlert("二级分类不能为空");
          return;
        }
        if (this.form.goodsid == "") {
          errorAlert("商品不能为空");
          return;
        }
        reslove();
      });
    },
    //点击了编辑按钮 ，获取一条数据 赋值
    async getOne(id) {
      this.data = [];
      let res = await reqseckinfo({ id });
      if (res.data.code === 200) {
        this.form = res.data.list;
        //  补id
        this.form.id = id;
        this.data = [
          new Date(Number(this.form.begintime)),
          new Date(Number(this.form.endtime)),
        ];
      }
    },
    //清空
    empty() {
      this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: "",
      };
      this.data = [];
    },
    //更改
    updata() {
      this.form.begintime = this.data[0];
      this.form.endtime = this.data[1];
      reqseckedit(this.form).then((res) => {
        this.checkeddata().then(() => {
          if (res.data.code == 200) {
            successAlert(res.data.msg);
            this.empty();
            this.status.isshow = false;
            this.reqList();
          }
        });
      });
    },
    // 9.弹框关闭
    closed() {
      // 如果是添加的取消，什么都不做；如果是编辑的取消，form清空
      if (!this.status.isAdd) {
        this.empty();
      }
    },
  },

  mounted() {
    if (this.cateList.length == 0) {
      this.reqCateList();
    }
  },
  computed: {
    ...mapGetters({
      //分类的列表数据
      cateList: "cate/list",
    }),
  },
  watch: {},
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed @primary;
  position: relative;
}
.my-upload-plus {
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 100px;
}
.my-upload-input {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
  opacity: 0;
}
.my-upload-img {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}
</style>
